package com.touzbi.hss.dao;

import java.util.List;

import org.hibernate.Session;

import com.touzbi.hss.beans.Patient;
import com.touzbi.hss.beans.Soin;

public class SoinDAO implements AbstractSoinDAO {

	private static final String HIBERNATE_LIST_QUERY = "from Soin";

	private static final String EXCEPTION_NO_SOIN = "No soin found";

	private AbstractDAOFactory daoFactory;

	public SoinDAO(AbstractDAOFactory daoFactory) {
		this.daoFactory = daoFactory;
	}

	private boolean isEmpty(List<Soin> list) {
		return list == null || list.isEmpty();
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Soin> getSoinList() throws DAOException {
		List<Soin> result = this.daoFactory.getSession()
				.createQuery(HIBERNATE_LIST_QUERY).list();
		if (isEmpty(result)) {
			throw new DAOException(EXCEPTION_NO_SOIN);
		}
		return result;
	}

	@Override
	public void addSoin(Soin soin) {
		this.daoFactory.getSession().save(soin);
	}

	@Override
	public void addPatient(Patient patient, Soin soin) {
		Session session = this.daoFactory.getSession();
		session.save(patient);
		session.save(soin);
		soin.addPatient(patient);
	}

}
